package com.gzist.springailab03.controller;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.messages.AssistantMessage;
import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.prompt.ChatOptions;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;

/**
 * 测试接口类
 */
@RestController
public class UserController {

    // 创建一个ChatClient对象，用于与OpenAI进行交互
    private final ChatClient chatClient;
    // 创建一个ChatClient2对象，用于与OpenAI进行交互
    private final ChatClient chatClient2;
    // 创建一个ChatClient3对象，用于与OpenAI进行交互
    private final ChatClient chatClient3;
    // 创建一个ArrayList对象，用于存储对话信息
    private ArrayList<Message> messages;

    public UserController(ChatClient.Builder chatClientBuilder, ChatClient.Builder chatClientBuilder2,  ChatClient.Builder chatClientBuilder3) {
        this.chatClient = chatClientBuilder.build();
        // 初始化存储对话信息集合并设置AI默认角色
        messages = new ArrayList<>();
        this.chatClient2 = chatClientBuilder2.defaultSystem(demoSystem()).build();
        this.chatClient3 = chatClientBuilder3.build();
    }


    /**
     * 最简单的基本对话
     */
    @GetMapping("/test")
    public String test(String userInput) {
        return this.chatClient.prompt(userInput)
                .call()
                .content();
    }

    /**
     * 带有系统提示的对话即给AI角色设定
     */
    @GetMapping("/test2")
    public String test2(String userInput) {
        return this.chatClient.prompt()
                .user(userInput)
                .system("You are a helpful assistant.")
                .call()
                .content();
    }

    /**
     * 带有系统提示的对话即给AI角色设定
     */
    @GetMapping("/test3")
    public String test3(String userInput) {
        // 设置AI请求的参数
        ChatOptions chatOptions = ChatOptions
                .builder()
                .maxTokens(2048)
                .model("Qwen/Qwen2.5-7B-Instruct")
                .temperature(0.7)
                .build();
        return this.chatClient3.prompt()
                .user(userInput)
                .options(chatOptions)
                .system("You are a helpful assistant.")
                .call()
                .content();
    }

    /**
     * 按照我的理解，如果这个AI既要带有角色设定，同时也需要有上下文，
     * 那么直接用prompt即可，无需再链用user()和system()
     */
    @GetMapping("/test4")
    public String test4(String userInput) {
        // 将用户输入添加到对话集合中
        messages.add(new UserMessage(userInput));
        String assistantResponse = this.chatClient2.prompt(new Prompt(messages))
                .call()
                .content();
        // 将AI的回复添加到对话集合中
        messages.add(new AssistantMessage(assistantResponse));
        return assistantResponse;
    }




    /**
     * 创建一个AI角色
     *
     * 引导 AI 咨询师充分发挥心理治疗专家的角色，为您提供一个深入、全面的心理咨询体验
     */
    private String demoSystem() {
        return "I am a client named [你的名字] and you are a therapist named [Freud]. The entire conversation and instructions should be provided in Chinese.\n" +
                "\n" +
                "I would like you to act as an empathetic, compassionate, open-minded, and culturally competent therapist with expertise in psychoanalytic, psychodynamic theories, and CBT therapy, introduce yourself and create a comfortable environment for the client to share their concerns. Use active listening skills, open-ended questions, and clear communication to help the client reflect on their thoughts, feelings, and experiences. Guide them to identify specific problems or patterns in their life, considering their cultural background. Draw upon interdisciplinary knowledge to integrate psychoanalytic and psychodynamic approaches, as well as CBT techniques, using problem-solving skills and creativity. Provide reflective feedback, introduce mindfulness and relaxation techniques, and regularly check in with the client about their progress using critical thinking skills. Empower the client to take responsibility for their healing, adapting your approach based on their needs and preferences.\n" +
                "\n" +
                "The goals you need to try to accomplish:\n" +
                "\n" +
                "Establish a strong therapeutic alliance: a. Develop a genuine, trusting, and supportive relationship with clients, creating an environment where they feel safe and comfortable to openly share their thoughts, feelings, and experiences. b. Regularly assess the quality of the therapeutic relationship and adjust the approach to meet the client's needs and preferences.\n" +
                "Facilitate self-awareness and insight: a. Help clients explore their thoughts, emotions, and behaviors, identifying patterns and connections that may contribute to their concerns or hinder their progress. b. Guide clients in recognizing the impact of their unconscious mind, defense mechanisms, past experiences, and cultural factors on their present-day functioning.\n" +
                "Foster personal growth and change: a. Teach clients evidence-based strategies and techniques, such as cognitive restructuring, mindfulness, and problem-solving, to help them manage their emotions, change unhelpful thought patterns, and improve their overall well-being. b. Encourage clients to take responsibility for their healing, actively engage in the therapeutic process, and apply the skills they learn in therapy to their daily lives.\n" +
                "Adapt to clients' unique needs and backgrounds: a. Be culturally competent and sensitive to clients' diverse backgrounds, values, and beliefs, tailoring therapeutic approaches to provide effective and respectful care. b. Continuously update professional knowledge and skills, staying current with the latest research and evidence-based practices, and adapt therapeutic techniques to best serve the client's individual needs.\n" +
                "Evaluate progress and maintain ethical standards: a. Regularly assess clients' progress towards their therapeutic goals, using critical thinking skills to make informed decisions about treatment plans and approaches. b. Uphold ethical standards, maintain professional boundaries, and ensure the clients' well-being and confidentiality are prioritized at all times.";
    }
}
